var globalCount = 2;
var pageNext = 1;
var isEnd = false;
$(".top").click(function(){
    $('body,html').animate({scrollTop:0},1000);
    return false;
});
	var width = window.innerWidth || document.documentElement.clientWidth; 
	if (width < 700) {
		$(".top").css("display","none");
	}
	$(window).scroll(function(){
		if($(document).scrollTop()>800&&width>700){
			$(".top").css("display","block");
		}else{
			$(".top").css("display","none");
		}
	});
	$(document).ready(function() {
	    initBlogByNew();	
	    selectPingList(pageNext);
	});
	
	$("#submit").click(function() {//button的click事件
	    addPing();
	});
	
	//初始化推荐
	var initBlogByNew=function(){
		//设置参数
		var params ={
				pageSize : 10,
			    pageIndex : 1,
			    status : 1,
			    isTop : 0,
			    isRecommend : 0,
			    blogTypeId : 0,
			    agreeNum : 0,
			    commentNum : 0,
			    clickNum : 0    
		};
		$.ajax({
            url:'../blogadmin/getbloglistbypage',    
            type:'get',
            data:params,
            dataType:'json',    
            success:function (dataAll) {
            	var newBlog='';
            	var data=dataAll.blogList;
            	var time='';
                for (var i = 0; i < data.length; i++) {
                	time=i*0.05;
                	newBlog+='<li style="animation-delay:'+time+'s" class="animated fadeIn"><b><a href="../frontend/info.html?blogId=' + data[i].blogId + '">' + data[i].blogTitle + '</a></b><p><span>' + data[i].blogIntroduction + '</span></p></li>';
                }
                // 初始化数据
                $(".paihang").find(".new").html(newBlog);
            },    
		    error:function(){
		    	layer.msg('加载的太快啦', {icon: 2});
		    }	
        });
	};
	
	var selectPingList = function(pageNum) {
		var params = {
				pageSize : 15,
				pageIndex : pageNum,
				blogId : -1,
				type : 2
			};
			$.ajax({
				url : '/ping/getpinglistbyblogid',
				type : 'post',
				data : params,
				dataType : 'json',
				success : function(dataAll) {
					if (dataAll.success) {
						var newPing = "";
						var pageCount = dataAll.count;
						var data = dataAll.pingList;
						for(var i = 0 ; i < data.length; i ++){
							newPing += '<span style="font-weight:700">' +  data[i].pingName + '</span>' + '[' + Format(data[i].createTime, "yyyy-MM-dd") + ']：' 
									+ data[i].pingContext + '<hr style="height:2px;border:none;border-top:1px dashed #000000;">';
						}
						if(pageNum >= parseInt(2)){
							$("#pinglunban").append(newPing);
						}else{
							$("#pinglunban").html(newPing);
						}
						if(pageCount > parseInt(30)){
							var pagenav = '';
					          if(page == dataAll.page){
					        	  isEnd = true;
						          pagenav = '<p style="text-align:center;margin:-5px auto 10px;"><a href="javascript:void(0);" onclick="window.scrollTo(0,0)"><i class="fa fa-arrow-up"></i> 没有更多了</a></p>';
						          if (width < 660) {
						            $(".pageMin").html(pagenav);
						          }
					          }else{
					        	  isEnd = false;
						          pageNext = parseInt(page) + parseInt(1);
						          pagenav = '<div style="margin:-5px auto 10px;text-align:center;"><div class="loader-inner ball-pulse"><div></div><div></div><div></div></div></div>';
					          }
						}
						$(".page").html(pagenav);
						$("#commentNum").html(pageCount);
					} else{
						$(".page").html("");
					}
				},
				error : function() {
					swal("查询错误", "请重新操作", "error");
				}
			});
	}
	
	var addPing = function() {
		var params = {
			blogId : $("#blogId").val(),
			pingName : $("#pinglunName").val(),
			pingContext : $(".comments").val(),
			type : 2,
			prarm : '留言成功！',
		};
		$.ajax({
			url : '../ping/addping',
			type : 'post',
			data : params,
			dataType : 'json',
			success : function(data) {
				if (data.success) {
					$("#pinglunName").val("");
					$(".comments").val("");
					swal("添加成功", "", "success");
				}
				selectPingList(1);
			},
			error : function() {
				swal("添加留言错误", "请重新操作", "error");
			}
		});
	};
	
	//格式化时间
	function Format(datetime, fmt) {
	  if (parseInt(datetime) == datetime) {
	    if (datetime.length == 10) {
	      datetime = parseInt(datetime) * 1000;
	    } else if (datetime.length == 13) {
	      datetime = parseInt(datetime);
	    }
	  }
	  datetime = new Date(datetime);
	  var o = {
	    "M+" : datetime.getMonth() + 1, //月份   
	    "d+" : datetime.getDate(), //日   
	    "h+" : datetime.getHours(), //小时   
	    "m+" : datetime.getMinutes(), //分   
	    "s+" : datetime.getSeconds(), //秒   
	    "q+" : Math.floor((datetime.getMonth() + 3) / 3), //季度   
	    "S" : datetime.getMilliseconds()
	  //毫秒   
	  };
	  if (/(y+)/.test(fmt))
	    fmt = fmt.replace(RegExp.$1, (datetime.getFullYear() + "")
	      .substr(4 - RegExp.$1.length));
	  for (var k in o)
	    if (new RegExp("(" + k + ")").test(fmt))
	      fmt = fmt.replace(RegExp.$1,
	        (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k])
	          .substr(("" + o[k]).length)));
	  return fmt;
	}